package com.bridgeintelligent.tag.customer.controller;

import com.bridgeintelligent.tag.customer.dto.CustomersDto;
import com.bridgeintelligent.tag.customer.entity.CustomersPage;
import com.bridgeintelligent.tag.customer.entity.Result;
import com.bridgeintelligent.tag.customer.exception.CustomersException;
import com.bridgeintelligent.tag.customer.model.IssueLogQueryModel;
import com.bridgeintelligent.tag.customer.service.IIssueLogService;
import com.bridgeintelligent.tag.customer.service.IIssueSendService;
import com.bridgeintelligent.tag.customer.vo.IssueLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @description
 * @author: grp 2021-02-23 13:52
 **/
@Api(tags = "下发日志")
@RestController
@AllArgsConstructor
@Validated
@Slf4j
@RequestMapping("api/issueLog")
public class IssueLogController {

    IIssueLogService issueLogService;
    IIssueSendService issueSendService;

    @ApiOperation(value = "下发日志列表", notes = "下发日志管理")
    @GetMapping("list")
    public CustomersDto<CustomersPage<IssueLogVO>> list(@ModelAttribute @Valid IssueLogQueryModel model) {
        model.setPageNumber(model.getPageNumber() == null ? 1 : model.getPageNumber());
        model.setPageSize(model.getPageSize() == null ? 10 : model.getPageSize());
        CustomersPage<IssueLogVO> page = issueLogService.list(model);
        return CustomersDto.success(page);
    }

    @ApiOperation(value = "重试", notes = "下发日志管理")
    @ApiImplicitParams({@ApiImplicitParam(paramType = "query", name = "logId", value = "日志id", required = true)})
    @GetMapping("retry")
    public CustomersDto<Object> retry(@NotBlank String logId) {
        try{
            issueSendService.sendByLogId(logId);
        } catch (SQLIntegrityConstraintViolationException e){
            return CustomersDto.error("有相同任务正在执行!");
        }catch (CustomersException e){
            return CustomersDto.error(e.getMessage());
        }
        return CustomersDto.success();
    }


    @ApiOperation(value = "导出下发异常状态处理", notes = "导出下发异常状态处理")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "logId", value = "日志id", required = true),
            @ApiImplicitParam(paramType = "query", name = "state", value = "状态", required = true),
            @ApiImplicitParam(paramType = "query", name = "reason", value = "失败原因", required = true)
    })
    @GetMapping("/test")
    public Result test(String logId, String state, String reason) {
        issueSendService.test(logId,state,reason);
        return new Result(0,"操作成功");
    }


    @ApiOperation(value = "判断同一下发任务是否有正在运行的任务", notes = "true:没有/false:有")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "taskId", value = "任务ID", required = true)
    })
    @GetMapping("/runCount")
    public Result runCount(String taskId) {
        boolean b = issueLogService.runCount(taskId);
        return new Result(0,"操作成功",b);
    }
}
